package com.lanou3g.baidumusic.main.mainactivity.addfragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lanou3g.baidumusic.R;
import com.lanou3g.baidumusic.main.mainactivity.BaseFragment;
import com.lanou3g.baidumusic.main.tools.NetResultInterface;
import com.lanou3g.baidumusic.main.tools.NetWorkInstance;

/**
 * Created by dllo on 17/3/29.
 */

public class AddNewFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.diy.gedanInfo&from=ios&&version=5.2.3&from=ios&channel=appstore&listid=365291392";
    private AddFragmentAdapter adapter;

    private AddFragmentReceiver receiver ;

    @Override
    public int setLayout() {
        return R.layout.fragment_add_new;
    }

    @Override
    public void initView(View view) {

        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_add_rv);
        adapter = new AddFragmentAdapter(getContext());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context , LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);


    }

    @Override
    public void initData() {

        recyclerView.setAdapter(adapter);

        receiver = new AddFragmentReceiver();
        //去过滤Intent这个意图的
        //通过构造方法中参action过滤intent
        String aaa =  Intent.ACTION_SEND;
        IntentFilter intentFilter = new IntentFilter("001");
        String s = intentFilter.getStringExtra("001");
        //注册receiver   注册接收器
        getContext().registerReceiver(receiver, intentFilter);

    }

    private void getData(String url){
        this.url = url ;

        NetWorkInstance.getInstance().netRequest(url, new NetResultInterface() {
            //当网络请求成功了,自动调用此方法
            @Override
            public void sucess(String str) {
//                Log.d("success", str);

                //gson解析
                Gson gson = new Gson();
                //解析后的数据存于homeEntity中
                AddFragmentBean addFragmentBean = gson.fromJson(str , AddFragmentBean.class);
//                homeEntity.getData().getItems().get(1).getIntroduction();
                adapter.setAddFragmentBean(addFragmentBean);

            }
            //网络请求失败了,自动调用次方法
            @Override
            public void faile(String str) {
                Log.d("faile" + "", str);
            }
        });

    }


    class AddFragmentReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            //接收广播,刷新页面
            getData();
            adapter.notifyDataSetChanged();
            Toast.makeText(context, "执行了", Toast.LENGTH_SHORT).show();
            Log.d("LetterReceiver", "成功了!");

        }
    }


}
